package com.css.fxfzaqbz.modules.assess.service.impl;

import com.css.fxfzaqbz.base.upload.entity.AttachmentInfoEntity;
import com.css.fxfzaqbz.base.upload.repository.AttachmentInfoRepository;
import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess.entity.*;
import com.css.fxfzaqbz.modules.assess.repository.CasualtiesRepository;
import com.css.fxfzaqbz.modules.assess.service.CasualtiesService;
import com.css.fxfzaqbz.modules.assess.vo.CasVo;
import com.css.fxfzaqbz.modules.assess2.entity.UrgencysettleData;
import com.css.fxfzaqbz.modules.assess2.repository.UrgencysettleDataRepository;
import com.css.fxfzaqbz.util.*;
import com.css.fxfzaqbz.web.PlatformSessionContext;
import com.css.fxfzaqbz.zcpt.sys.entity.SUser;
import com.css.fxfzaqbz.zcpt.sys.service.SUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author y1fan
 * @create 2022-09-13 14:52
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CasualtiesServiceImpl implements CasualtiesService {

    @Resource
    private CasualtiesRepository casualtiesRepository;
    @Resource
    private SUserService sUserService;
    @Resource
    private UrgencysettleDataRepository urgencysettleDataRepository;
    @Resource
    private AttachmentInfoRepository attachmentInfoRepository;

    @Override
    @Transactional
    public CasualtiesEntity saveCasualties(CasualtiesEntity casualtiesEntity) {

        casualtiesEntity.setScDate(PlatformDateUtils.getCurrentTimestamp());
        casualtiesEntity.setScId(UUIDGenerator.getUUID());
        casualtiesEntity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        casualtiesEntity.setUpdateTime(null);
        casualtiesEntity.setDelFlag(YNEnum.N.toString());
        String userID = PlatformSessionContext.getUserID();
        casualtiesEntity.setCreateUser(userID);
        casualtiesEntity.setTypes("人员伤亡评估");
        casualtiesEntity.setMark(YNEnum.N.toString());

        String userJson = sUserService.getSUser(userID);
        SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
        if (sUser != null) {

            casualtiesEntity.setScPeople(sUser.getUserName());
        }

       /* //保存大表对象
        BigCasualtiesEntity bigCasualtiesEntity = bigCasualtiesRepository.getBigCasualtiesByActiveId(casualtiesEntity.getScRelation());
        if (bigCasualtiesEntity == null) {

            BigCasualtiesEntity bigCasualtiesEntity1 = new BigCasualtiesEntity();
            bigCasualtiesEntity1.setBcId(UUIDGenerator.getUUID());
            bigCasualtiesEntity1.setBcActiveId(casualtiesEntity.getScRelation());
            bigCasualtiesEntity1.setBcStatus("0");
            bigCasualtiesEntity1.setBcCreateTime(PlatformDateUtils.getCurrentTimestamp());
            bigCasualtiesEntity1.setBcDelFlag(YNEnum.N.toString());
            bigCasualtiesEntity1.setBcCreateUser(PlatformSessionContext.getUserID());
            bigCasualtiesEntity1.setContent("人员伤亡评估");
            bigCasualtiesEntity1.setBcOpinion(importantActivityService.getImportantActivityById(casualtiesEntity.getScRelation()).getIaOpinion());
            bigCasualtiesRepository.saveCasualties(bigCasualtiesEntity1);
        }*/

        return casualtiesRepository.saveCasualties(casualtiesEntity);


    }

    @Override
    public List<CasualtiesEntity> getAllCasualties(String id) {
        return casualtiesRepository.getAllCasualties(id);
    }

    @Override
    public CasualtiesEntity getCasualtiesById(String id) {
        CasualtiesEntity casualtiesEntity = casualtiesRepository.getCasualtiesById(id);
        if (!PlatformObjectUtils.isEmpty(casualtiesEntity.getScFileDescribe())) {
            List<String> list = dataString(casualtiesEntity.getScFileDescribe());
            casualtiesEntity.setDescribe(list);
        }

        return casualtiesEntity;
    }

    @Override
    @Transactional
    public void deleteCasualties(String id) {
        String userID = PlatformSessionContext.getUserID();
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        casualtiesRepository.deleteCasualties(id, userID, currentTimestamp);

    }

    @Override
    @Transactional
    public CasualtiesEntity getCasualtiesByType(CasualtiesEntity casualtiesEntity) {
       /* CasualtiesEntity casualtiesByType = casualtiesRepository.getCasualtiesByType(casualtiesEntity.getScAssContent(), casualtiesEntity.getScRelation());
        if (casualtiesByType != null) {
            casualtiesService.deleteCasualties(casualtiesByType.getScId());
            CasualtiesEntity casualtiesEntity1 = casualtiesService.saveCasualties(casualtiesEntity);
            return casualtiesEntity1;
        } else {
            CasualtiesEntity casualtiesEntity1 = casualtiesService.saveCasualties(casualtiesEntity);
            return casualtiesEntity1;
        }*/
        return null;
    }

    @Override
    @Transactional
    public CasualtiesEntity updateCasualties(CasualtiesEntity casualtiesEntity, String scId) {


        String userID = PlatformSessionContext.getUserID();
        casualtiesEntity.setCreateUser(userID);
        casualtiesEntity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        casualtiesEntity.setScDate(PlatformDateUtils.getCurrentTimestamp());
        return casualtiesRepository.updateCasualties(casualtiesEntity, scId);
    }

    @Override
    @Transactional
    public CasualtiesEntity updateCasualtiesById(CasualtiesEntity casualtiesEntity) {
        casualtiesEntity.setScDate(PlatformDateUtils.getCurrentTimestamp());
        casualtiesEntity.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        String userID = PlatformSessionContext.getUserID();
        casualtiesEntity.setUpdateUser(userID);

        return casualtiesRepository.updateCasualtiesById(casualtiesEntity);
    }


    /*
     * 查询最终成果
     * */
    @Override
    public List<CasualtiesEntity> getAllCasualtiesTwo(String id) {
        List<CasualtiesEntity> allCasualtiesTwo = casualtiesRepository.getAllCasualtiesTwo(id);
        for (CasualtiesEntity casualtiesEntity : allCasualtiesTwo) {
            if (!PlatformObjectUtils.isEmpty(casualtiesEntity.getScFileDescribe())) {
                List<String> list = dataString(casualtiesEntity.getScFileDescribe());
                casualtiesEntity.setDescribe(list);
            }
        }
        return allCasualtiesTwo;
    }

    @Override
    public void firstSubmitCasualties(String id) {
        String userID = PlatformSessionContext.getUserID();
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        CasualtiesEntity casualtiesById = casualtiesRepository.getCasualtiesById(id);
        List<CasualtiesEntity> casualtiesEntities = casualtiesRepository.getCasualtiesByType(casualtiesById.getScAssContent(), casualtiesById.getScRelation());


        if (casualtiesEntities != null) {

            if (casualtiesEntities.get(0).getScFileDescribe() != "") {
                casualtiesById.setScFileDescribe(casualtiesEntities.get(0).getScFileDescribe());
                casualtiesRepository.firstSubmitCasualties(id, userID, currentTimestamp, casualtiesById.getScFileDescribe());
            } else {
                casualtiesRepository.firstSubmitCasualties2(id, userID, currentTimestamp);
            }
        } else {
            casualtiesRepository.firstSubmitCasualties2(id, userID, currentTimestamp);
        }
    }

    @Override
    public Boolean casualtiesIfRepetition(CasualtiesEntity casualtiesEntity) {
        CasualtiesEntity casualtiesEntity1 = casualtiesRepository.casualtiesIfRepetition(casualtiesEntity);
        if (casualtiesEntity1 == null) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public void addCasualtiesDisaster(List<String> ids, String describe) {
        casualtiesRepository.addCasualtiesDisaster(ids, describe);
    }

    @Override
    public void removeCasualtiesDisaster(String id) {
        CasualtiesEntity casualtiesById = casualtiesRepository.getCasualtiesById(id);
        casualtiesById.setMark(YNEnum.N.toString());
        casualtiesById.setScFileDescribe("");
        casualtiesById.setSubmiter("");
        casualtiesById.setSubmitPhone("");
        casualtiesRepository.removeCasualtiesDisaster(casualtiesById);

    }

    @Override
    public void secondSubmitCasualties(List<String> ids) {
        String userJson = sUserService.getSUser(PlatformSessionUtils.getUserId());
        SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        casualtiesRepository.secondSubmitCasualties(ids, sUser.getMobilePhone(), sUser.getUserName(), currentTimestamp);
    }

    @Override
    public List<CasualtiesEntity> getCasByIaIdAndType(String iaId, String rkfbt) {
        return casualtiesRepository.getCasByIaIdAndType(iaId, rkfbt);
    }

    @Override
    public List<CasAndUrgen> getAll(CasVo vo) {
        return casualtiesRepository.getAll(vo);
    }

    @Override
    public void submit(CasVo vo) {
        casualtiesRepository.submit(vo);
    }

    @Override
    public AttachmentInfoEntity getPath(CasVo vo) {
        AttachmentInfoEntity attach = null;
        if ("需紧急安置人员".equals(vo.getCont())) {
            UrgencysettleData urgencysettleData = urgencysettleDataRepository.getUrgencysettleDataById(vo.getId());
            String udFileId = urgencysettleData.getUdFileId();
            attach = attachmentInfoRepository.getAttach(udFileId);
        } else {
            CasualtiesEntity casualtiesEntity = casualtiesRepository.getCasualtiesById(vo.getId());
            attach = attachmentInfoRepository.getAttach(casualtiesEntity.getScFileId());
        }
        return attach;
    }

    @Override
    public void delete(CasVo vo) {
        String userId = PlatformSessionUtils.getUserId();
        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        if ("需紧急安置人员".equals(vo.getCont())) {
            urgencysettleDataRepository.deleteUrgencysettleData(vo.getId(),userId,timestamp);
        }else {
            casualtiesRepository.deleteCasualties(vo.getId(),userId,timestamp);
        }
    }

    @Override
    public void remove(CasVo vo) {
        casualtiesRepository.remove(vo);

    }


    /*
     * 字符串转集合方法
     * */
    private List<String> dataString(String sdaAssFileDescribe) {
        String[] split = sdaAssFileDescribe.split("@@@");
        List<String> disList = new ArrayList<>();
        Collections.addAll(disList, split);
        return disList;
    }

}
